#include "ValidatorUtils.h"

ValidatorUtils::ValidatorUtils()
{

}

ValidatorUtils::~ValidatorUtils()
{

}

bool ValidatorUtils::converToInt (const char* value, int* intValue) throw (InvalidIntegerException){		
	int val;
	char valueString;
	char* stringValue;
	string valueWithoutSpaces = value;
	char* newValue = "00000000";

	if(valueWithoutSpaces[0] == ' '){
		valueWithoutSpaces = valueWithoutSpaces.substr(1, valueWithoutSpaces.size() -1);
	}

	if(valueWithoutSpaces[valueWithoutSpaces.size() -1] == ' '){
		valueWithoutSpaces = valueWithoutSpaces.substr(0, valueWithoutSpaces.size() -1);
	}	

	val = atoi(valueWithoutSpaces.c_str());

	stringValue = itoa(val, &valueString, 10);

	if(!isNumber(valueWithoutSpaces.c_str())) throw InvalidIntegerException("El valor ingresado no es un entero");

	if (strcmp(valueWithoutSpaces.c_str(), stringValue) != 0){
		*intValue = 0;
		return false;
	}
	else{
		*intValue = val;
		return true;
	}
	
}

bool Exists(list<string> idList, string idValue){
	list<string>::iterator it;
	string value;
	for (it= idList.begin() ; it!=idList.end(); it++){	
		value = *it;
		if (value == idValue)
			return true;
	}
	return false;
}

int ValidateObjects(list<MapList> xmlObjects)
{
	MapList mapList;
	map<string, CustomizedMap> types;
	map<string, CustomizedMap>::iterator ii;
	map<string, string>::iterator jj;
	list<MapList>::iterator it;
	list<string> idList;
	string id = "id";
	string idValue;
	string attribute;

	for (it= xmlObjects.begin() ; it!=xmlObjects.end(); it++)
	{	
		cout << endl;
		mapList = *it;
		types = mapList.mapList;
		

    	for(ii=types.begin(); ii!=types.end(); ++ii){
		   cout << (*ii).first << " ";
		   CustomizedMap map = ((CustomizedMap)(*ii).second);

		   
		   for( jj=map.customMap.begin(); jj!=map.customMap.end(); ++jj) {
				attribute = (*jj).first;
				if (attribute == id){									
					idValue =  (*jj).second;				
					if (Exists(idList, idValue)){
						return 1;
					}
					else{
						idList.push_back(idValue);
					}				
				}
			}
		   cout << endl;
		}
	}
	return 0;
}

bool ValidatorUtils::isNumber(std::string value){
	int lenght = value.size();

	for(int i=0;i<lenght;i++){
		if(!isdigit(value[i]))
			return false;
	}
	return true;
}

bool ValidatorUtils::isNumber(const char* value){
	int lenght = strlen(value);

	for(int i=0;i<lenght;i++){
		if(!isdigit(value[i]))
			return false;
	}
	return true;
}